package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"quantitative/model"
	"quantitative/position"
	"quantitative/strategy"
)

/**
获取数据
 */
func getData() (map[string]interface{}) {
	b, err := ioutil.ReadFile("./data/SH600570-16-18.json")
	//b, err := ioutil.ReadFile("./data/SH600570-16-17.json")
	//b, err := ioutil.ReadFile("./data/SH600570-14-18.json")
	//b, err := ioutil.ReadFile("./data/XYF.json")
	if err != nil {
		fmt.Print(err)
	}
	jsonStr := string(b)

	var jsonData map[string]interface{}
	if err := json.Unmarshal([]byte(jsonStr), &jsonData); err != nil {
		panic(err)
	}

	return jsonData
}

func main() {
	//回测股票数据
	jsonData := getData()

	//持仓
	var holdPosition position.HoldPosition


	//设置原始持仓数据
	holdPosition.First(0, 0)


	//总利润
	var totalProfit int64 = 0

	//前一个交易日数据
	var preShare model.Share
	for _, chartItem := range jsonData["chartlist"].([]interface{}) {
		//回测数据 每个交易日股票数据
		var oneShare model.Share
		oneShare.SetShare(chartItem, preShare)
		//替换
		preShare = oneShare

		//均线策略
		var twin strategy.TwinAverage

		totalProfit += twin.Run(oneShare, &holdPosition)

		//风险控制
		//if holdPosition.Num >= 100 && oneShare.Open < (holdPosition.Cost * 95 / 100) {
		//	fmt.Println("***************风险控制清仓*****************")
		//
		//	var timeHelper common.TimeHelper
		//	riskProfit := (oneShare.Open - holdPosition.Cost) * 100
		//	totalProfit += riskProfit
		//	holdPosition.Cell(oneShare.Open, twin.TransNum())
		//
		//	fmt.Println(timeHelper.Format(oneShare.Timestamp)," | ",  float64(oneShare.Open) / 100, "元卖出 | 本次持仓盈利", float64(riskProfit)/ 100)
		//}
	}

	fmt.Println("**********************")
	fmt.Println("**********************")

	fmt.Println("总持仓盈利", float64(totalProfit) / 100 , " 元")

	fmt.Println("**********************")
	fmt.Println("**********************")
}